package com.wyzz.hr.action;

import java.util.*;
import org.apache.axis.utils.StringUtils;
import org.hibernate.criterion.*;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.ISalaryPaidBo;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empsalarypay;
import com.wyzz.hr.pojo.Location;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.DateUtil;

public class DwrForSearchEmp extends BaseAction {

	public DwrForSearchEmp() {
	}

	public String searchEmp(String empStr, String deptStr, String local,
			String condition) {
		System.out.println(empStr+"==="+deptStr+"==="+local+"==="+condition);
		Department depart = null;
		if (!StringUtils.isEmpty(deptStr))
			depart = new Department(deptStr);
		Location location = null;
		if (!StringUtils.isEmpty(local))
			location = new Location(local);
		List empList = new ArrayList();
		empList = selectEmp(depart, empStr, location, condition);
		System.out.println(empList.size()+"pppppppppp");
		String result = "";
		for (Iterator it = empList.iterator(); it.hasNext();) {
			Employee emp = (Employee) it.next();
			if (!result.equals(""))
				result = (new StringBuilder()).append(result).append("&")
						.toString();
			result = (new StringBuilder()).append(result).append(
					emp.getEmpDistinctNo()).append(";")
					.append(emp.getEmpName()).append(";").append(emp.getId())
					.toString();
		}
		System.out.println(result + "oooooooo");
		return result;
	}

	private List selectEmp(Department depart, String empStr, Location location,
			String condition) {
		IEmployeeBo employeeBo = (IEmployeeBo) SpringBeanFactory
				.getBean("empBo");
		List empList = new ArrayList();
		DetachedCriteria dcEmp = DetachedCriteria.forClass(Employee.class);
		BaseCrit.addEmpDC(dcEmp, null, empStr);
		BaseCrit.addDeptDC(dcEmp, Employee.PROP_EMP_DEPT_NO,
				Employee.PROP_EMP_PB_NO, Integer.valueOf(1), depart);
		BaseCrit.addDC(dcEmp, Employee.PROP_EMP_LOCATION_NO, Location.PROP_ID,
				new Object[] { location });
		String conditionArray[] = condition.split("&");
		if (conditionArray[0].equals("empNeedCard")) {
			String empStatus = conditionArray[1];
			if (empStatus.equals("0"))
				dcEmp.add(Restrictions.eq(Employee.PROP_EMP_STATUS, Integer
						.valueOf(1)));
			dcEmp.add(Restrictions.or(Restrictions.eq(
					Employee.PROP_EMP_SHIFT_TYPE, Integer.valueOf(2)),
					Restrictions.eq(Employee.PROP_EMP_SHIFT_TYPE, Integer
							.valueOf(3))));
			dcEmp.addOrder(Order.asc(Employee.PROP_EMP_NAME));
			empList = employeeBo.findByCriteria(dcEmp);
		} else if (conditionArray[0].equals("empNotUser")) {
			dcEmp.add(Restrictions.eq("empStatus", new Integer(1)));
			dcEmp
					.add(Restrictions
							.sqlRestriction("emp_no not in (select ui_emp_no from userinfo) order by emp_name"));
			empList = employeeBo.findByCriteria(dcEmp);
		} else if (conditionArray[0].equals("empHasComp")) {
			DetachedCriteria dcPay = DetachedCriteria
					.forClass(Empsalarypay.class);
			dcPay.createAlias(Empsalarypay.PROP_ESP_EMPNO, "espEmpno", 1);
			dcPay.addOrder(Order.asc((new StringBuilder()).append("espEmpno.")
					.append(Employee.PROP_EMP_NAME).toString()));
			conditionArray[1] = conditionArray[1].replaceAll("-", "");
			BaseCrit.addDC(dcPay, Empsalarypay.PROP_ESP_YEARMONTH, "eq",
					new String[] { conditionArray[1] });
			BaseCrit.addEmpDC(dcPay, Empsalarypay.PROP_ESP_EMPNO, empStr);
			BaseCrit.addDeptDC(dcPay, Empsalarypay.PROP_ESP_DEPT,
					Empsalarypay.PROP_ESP_PB_NO, null, depart);
			BaseCrit.addDC(dcPay, Empsalarypay.PROP_ESP_LOCATION,
					Location.PROP_ID, new Object[] { location });
			ISalaryPaidBo salaryPaidBo = (ISalaryPaidBo) SpringBeanFactory
					.getBean("salaryPaidBo");
			List payList = salaryPaidBo.findByCriteria(dcPay);
			if (payList != null && !payList.isEmpty()) {
				Empsalarypay emppay;
				for (Iterator i$ = payList.iterator(); i$.hasNext(); empList
						.add(emppay.getEspEmpno()))
					emppay = (Empsalarypay) i$.next();

			}
		} else if (conditionArray[0].equals("empRequestAdd")) {
			String moduleNo = conditionArray[1];
			String auth = null;
			if ("4".equals(moduleNo))
				auth = DWRUtil.checkAuth("empLRAddInit", "executeInit");
			else if ("3".equals(moduleNo))
				auth = DWRUtil.checkAuth("trepAddInit", "trepAddInit");
			else
				return null;
			if ("SUB".equals(auth))
				addSubDC(dcEmp, "", Integer.parseInt(moduleNo));
			else if ("OWN".equals(auth))
				BaseCrit.addDC(dcEmp, Employee.PROP_ID, "eq",
						new String[] { getCurrentEmp().getId() });
			BaseCrit.addDC(dcEmp, Employee.PROP_EMP_STATUS, "eq",
					new Integer[] { Integer.valueOf(1) });
			dcEmp.addOrder(Order.asc(Employee.PROP_EMP_NAME));
			empList = employeeBo.findByCriteria(dcEmp);
		} else if (conditionArray[0].equals("leaveBalance")) {
			String year = String.valueOf(Calendar.getInstance().get(1));
			if (conditionArray.length > 1)
				year = conditionArray[1];
			java.util.Date startDate = DateUtil.getYearFirstDay(Integer
					.parseInt(year));
			java.util.Date endDate = DateUtil.getYearEndDay(Integer
					.parseInt(year));
			dcEmp.addOrder(Order.asc(Employee.PROP_EMP_NAME));
			dcEmp.add(Restrictions.le(Employee.PROP_EMP_JOIN_DATE, endDate));
			dcEmp.add(Restrictions.or(Restrictions.eq(Employee.PROP_EMP_STATUS,
					Integer.valueOf(1)), Restrictions.and(Restrictions.eq(
					Employee.PROP_EMP_STATUS, Integer.valueOf(0)), Restrictions
					.ge(Employee.PROP_EMP_TERMINATE_DATE, startDate))));
			empList = employeeBo.findByCriteria(dcEmp);
		} else if (conditionArray[0].equals("allEmp")) {
			String empStatus = conditionArray[1];
			dcEmp.addOrder(Order.asc(Employee.PROP_EMP_NAME));
			if (empStatus.equals("0"))
				dcEmp.add(Restrictions.eq(Employee.PROP_EMP_STATUS, Integer
						.valueOf(1)));
			empList = employeeBo.findByCriteria(dcEmp);
		} else if (conditionArray[0].equals("hasBeneType")) {
			String beneType = conditionArray[1].split(";")[0];
			String empStatus = conditionArray[1].split(";")[1];
			if ("1".equals(beneType))
				dcEmp.add(Restrictions
						.isNotNull(Employee.PROP_EMP_BENEFIT_TYPE));
			if (empStatus.equals("0"))
				dcEmp.add(Restrictions.eq(Employee.PROP_EMP_STATUS, Integer
						.valueOf(1)));
			dcEmp.addOrder(Order.asc(Employee.PROP_EMP_DISTINCT_NO));
			empList = employeeBo.findByCriteria(dcEmp);
		}
		return empList;
	}
}
